package com.guit.hrms.utils;

import com.guit.hrms.mapper.AcademyMapper;
import com.guit.hrms.mapper.DepartmentMapper;
import com.guit.hrms.mapper.InterviewMapper;
import com.guit.hrms.mapper.IntervieweeMapper;
import com.guit.hrms.po.academy.PlanAcademyJob;
import com.guit.hrms.po.department.PlanDeptJob;
import com.guit.hrms.po.interview.InterviewSituation;
import com.guit.hrms.po.interview.Interviewee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CalculateTheData {
    @Autowired
    private InterviewMapper interviewMapper;
    @Autowired
    private IntervieweeMapper intervieweeMapper;
    @Autowired
    private AcademyMapper academyMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 计算综合能力的加权算法
     *
     * @param interviewSituation 面试情况
     * @return
     */
    public Integer weightedAverage(InterviewSituation interviewSituation) {

        Integer weightedAverage = 0;
        //获取面试id
        Integer id = interviewSituation.getId();
        //获取三个分
        Integer professionalSkills = interviewSituation.getProfessionalSkills();
        Integer communicationSkills = interviewSituation.getCommunicationSkills();
        Integer teamworkAbility = interviewSituation.getTeamworkAbility();
        //根据面试id查询权重
        //查询应聘者id
        Integer intervieweeId = interviewMapper.getIntervieweeByInterviewId(id);
        //根据应聘者id查询部门/学院表三个主键
        Interviewee interviewee = intervieweeMapper.getPrimaryKey(intervieweeId);
        PlanAcademyJob planAcademyJob = academyMapper.getPlanAcademyJobByPrimaryKey(interviewee);
        //若查询学院为空则查询部门
        if (planAcademyJob == null) {
            PlanDeptJob planDeptJob = departmentMapper.getPlanDeptJobByPrimaryKey(interviewee);
            if (planDeptJob == null) {
                return weightedAverage;
            }
            Integer professionalSkillsRight = planDeptJob.getProfessionalSkillsRight();
            Integer communicationSkillsRight = planDeptJob.getCommunicationSkillsRight();
            Integer teamworkAbilityRight = planDeptJob.getTeamworkAbilityRight();

            weightedAverage =
                    compute(professionalSkills, communicationSkills, teamworkAbility,
                            professionalSkillsRight, communicationSkillsRight, teamworkAbilityRight);


            return weightedAverage;
        }
        Integer professionalSkillsRight = planAcademyJob.getProfessionalSkillsRight();
        Integer communicationSkillsRight = planAcademyJob.getCommunicationSkillsRight();
        Integer teamworkAbilityRight = planAcademyJob.getTeamworkAbilityRight();

        weightedAverage =
                compute(professionalSkills, communicationSkills, teamworkAbility,
                        professionalSkillsRight, communicationSkillsRight, teamworkAbilityRight);
        return weightedAverage;
    }

    /**
     * 计算加权平均数
     * 分
     *
     * @param professionalSkills
     * @param communicationSkills
     * @param teamworkAbility          权
     * @param professionalSkillsRight
     * @param communicationSkillsRight
     * @param teamworkAbilityRight
     * @return
     */
    public static Integer compute(Integer professionalSkills, Integer communicationSkills, Integer teamworkAbility,
                                  Integer professionalSkillsRight, Integer communicationSkillsRight, Integer teamworkAbilityRight) {

        return (professionalSkills * professionalSkillsRight +
                communicationSkills * communicationSkillsRight +
                teamworkAbility * teamworkAbilityRight) /
                (professionalSkillsRight + communicationSkillsRight + teamworkAbilityRight);
    }
}
